package com.starbaba.channelmanager.view;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.Action;
import com.starbaba.channelmanager.model.Product;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.core.web.struts2.JsonResult;
import com.starbaba.model.dictionary.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import java.io.PrintWriter;
import java.util.List;

/**
 * 产品渠道管理
 * @author Administrator
 *
 */
@SuppressWarnings({"deprecation","rawtypes"})
public class ProductChannelAction extends BaseAction {
    
    
    private static final long serialVersionUID = 1L;

    public String showPage () throws AppException {
		//在基础上添加产品列表
		BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
		String sql = " select DISTINCT prdid as code,name as kindname,isapp from comm_product_prdid  order by prdid  ";
		List<Product> productList = baseDao.list(sql, Product.class);
		paramsMap.put("productList",productList);
		return Action.SUCCESS;
	}
	
	
	@SuppressWarnings("unchecked")
    public void queryInfo() {
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);  //将请求参数封装带paramsMap中
		String code = (String)paramsMap.get("code");
		try {
			parsePageRequest();
			int pageSize = pageRequest.getIDisplayLength();
			int pageIndex = pageRequest.getIDisplayStart();
			
            PageResponse pageResponse = new PageResponse();
			BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(" select id,prdid as code,name as kindname,isapp,parent_id ,memo from comm_product_prdid where 1=1");
			
			if(StringUtils.isNotEmpty(code)) {
				stringBuilder.append(" and prdid = "+code+" ");
			}
			
			String limitSql = " limit "+pageIndex+","+pageSize+" ";
			List dataList = baseDao.list(stringBuilder.toString()+limitSql,Product.class);
			
			pageResponse.setAaData(dataList);
			pageResponse.setSEcho(pageRequest.getSEcho());
			List tmpDataList = baseDao.list(stringBuilder.toString(), Product.class);
			pageResponse.setITotalDisplayRecords(tmpDataList.size());
			pageResponse.setITotalRecords(tmpDataList.size());
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
	
	
	public String getInfo() {
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		try {
			
			BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
			String sql = " select prdid as code,name as kindname,isapp,parent_id,memo  from comm_product_prdid where id = "+id+" limit 1 ";
			Product product = baseDao.get(sql,Product.class);
			
			if (null != product && null != id) {
				paramsMap.put("id",id);
			}
			if (null != product && null != product.getCode()) {
				paramsMap.put("code", product.getCode());
			}
			if (null != product && null != product.getKindname()) {
				paramsMap.put("kindname",product.getKindname());
			}
			if (null != product) {
				paramsMap.put("isapp",product.getIsapp());
			}
			if (null != product && null != product.getParent_id()) {
				paramsMap.put("parent_id",product.getParent_id());
			}
			return Action.SUCCESS;
			
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		return null;
	}
	
	
	public void editInfo() throws Exception {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		String code = (String) paramsMap.get("code");
		String parentProdId = (String) paramsMap.get("parent_prod_id");
		String kindname = (String)paramsMap.get("kindname");
		String isapp = (String)paramsMap.get("isapp");
		String memo = (String)paramsMap.get("memo");
		BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
		
		if (StringUtils.isEmpty(isapp)) {
			isapp = "0";
		}
		
		if (!"-1".equals(id)) { //更新
			String sql = "update comm_product_prdid set prdid = ?,name = ?,isapp=?,parent_id=?,memo = ?  where id = ?";
			baseDao.update(sql, Integer.valueOf(code),kindname,Integer.parseInt(isapp),parentProdId,memo,Long.valueOf(id));
		} else { //添加
			String sql = "insert into comm_product_prdid(prdid,name,isapp,parent_id,memo) values(?,?,?,?,?)";
			baseDao.add(sql,Integer.valueOf(code),kindname,Integer.parseInt(isapp),parentProdId,memo);
		}
		
		sendJsonMessage(new JsonResult("1"));
	}
	
	//判断产品prdid是否存在
	public void isExist() {
		
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			
			BaseDao baseDao = new BaseDao(Constant.POOL_LIFE);
			String code = (String) paramsMap.get("code");
			//推广链接
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("select id");
			sbSql.append(" from comm_product_prdid");
			sbSql.append(" where prdid = ?");
			
			boolean result = true;
			
			Product model = baseDao.get(sbSql.toString(), Product.class, Integer.parseInt(code));
			
			if (model == null) {
				result = false;
			}
			
			if (result == true) {
				
				PrintWriter pw = ServletActionContext.getResponse().getWriter();
				String judge = "true";
				pw.write(judge);
				pw.flush();
				pw.close();
			}
			
		} catch(Exception e) {
			e.printStackTrace();
			LogService.error("getGeneraldata", e);
		}
		
	}
}
